﻿<%@ WebHandler Language="C#" Class="demo" %>

using System;
using System.Web;
using System;
using System.Web;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

public class demo : IHttpHandler {

    public void ProcessRequest(HttpContext context){
        context.Response.ContentType = "text/json";
        string methodName = context.Request["method"];
        if (methodName == "savedevice"){
            //获取前台ajax传过来的设备信息数据
            string deviceinfo = context.Request["deviceinfo"];
            //将前台传过来的json格式装换为String类型
            JObject jo = (JObject)JsonConvert.DeserializeObject(deviceinfo);
            //将基本信息存入数据库
            string sysid = jo["sysid"].ToString();
            string code = jo["code"].ToString();
            string msg = jo["msg"].ToString();
            string dayseconds = jo["dayseconds"].ToString();
            string date = jo["date"].ToString();
            string endtime = jo["endtime"].ToString();
            string runstatu = jo["runstatu"].ToString();
            string alarmstatu = jo["alarmstatu"].ToString();
            string systemtime = jo["systemtime"].ToString();
            string strSql = "insert into history_device(code,msg,dayseconds,dates,endtime,runstatu,alarmstatu,systemtime,sysid)" +
                            "values('" + code + "','" + msg + "','" + dayseconds + "','" + date + "','" + endtime + "','" + runstatu + "','" + alarmstatu + "','" + systemtime + "','"+sysid+"')";
            YunCutDBHelper.ExecuteSql(strSql);
        }else if (methodName == "savedata"){
            //获取前台ajax传过来的设备信息数据
            string datas = context.Request["datas"];
            //将前台传过来的json格式装换为String类型
            JObject jo = (JObject)JsonConvert.DeserializeObject(datas);
            //将基本信息存入数据库
            //查询主表id
            int did = (int)YunCutDBHelper.GetSingle("select top 1 id from history_device order by id desc");
            //将data参数值存入数据库
            string name = "";
            if (datas.IndexOf("\"name\":")>-1){
                name = jo["name"].ToString();
            }
            string runstatu1 = "";
            if (datas.IndexOf("\"runstatu1\":")>-1){
                runstatu1 = jo["runstatu1"].ToString();
            }
            string alarmstatu1 = "";
            if (datas.IndexOf("\"alarmstatu1\":")>-1){
                alarmstatu1 = jo["alarmstatu1"].ToString();
            }
            string strSql = "insert into history_device_data(name,runstatu,alarmstatu,did) values ('" + name + "','" + runstatu1 + "','" + alarmstatu1 + "'," + did + ")";
            YunCutDBHelper.ExecuteSql(strSql);
        }
        else if (methodName == "querytoken")
        {//判断是否为查询token和sysid的方法
         //查询从数据库获取到最后token指令
            DataSet a = YunCutDBHelper.Query("select top 1 token from token order by id desc");
            //查询获取到所有的sysid并进行循环
            DataSet aa = YunCutDBHelper.Query("select sysid from device ");
            //查询获取到所有的历史时间并进行循环
            DataSet aaa = YunCutDBHelper.Query("select name from history_between ");
            //遍历循环DataSet结果集
            string sysidList = "";
            foreach (DataRow dr in a.Tables[0].Rows){
                sysidList = sysidList + "{ \"token\":\"" + dr[0].ToString() + "\",\"sysids\":[";
            }
            foreach (DataRow dr in aa.Tables[0].Rows){
                sysidList = sysidList + "{ \"sysid\":\"" + dr[0].ToString() + "\"},";
            }
            sysidList = sysidList.Substring(0, sysidList.Length - 1);//去掉最后的一个逗号
            sysidList = sysidList + "],\"names\":[";
            foreach (DataRow dr in aaa.Tables[0].Rows){
                sysidList = sysidList + "{ \"name\":\"" + dr[0].ToString() + "\"},";
            }
            sysidList = sysidList.Substring(0, sysidList.Length - 1);//去掉最后的一个逗号
            sysidList = sysidList + "]}";
            context.Response.Write(sysidList.ToString());
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}